#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_i2c.h"
#include "iot_gpio.h"
#include "iot_errno.h"
#include "hi_io.h"
#include "oled.h"
#include "globalvar2.h"

void Oled_task()
{
    uint32_t retval = 0;
    static char line[32] = {0};

    OledInit();
    OledFillScreen(0);
    while(1) {
        snprintf(line, sizeof(line), "TEMP: %.2f", g_temp);
        OledShowString(0, 0, line, 2);

        snprintf(line, sizeof(line), "HUMI: %.2f ", g_humi);
        OledShowString(0, 2, line, 2);
        // snprintf(line, sizeof(line), "GAS: %.2f ", g_gas);
        // OledShowString(0, 2, line, 2);

        // snprintf(line, sizeof(line), "GAS: %.2f mg/m3", g_gas);
        // OledShowString(0, 4, line, 2);
        if(g_temp > 30.0)
        {
            OledShowString(0, 4, "Temp Abnormal!!", 2);
        }
        else
        {
            OledShowString(0, 4, "                ", 2); // 清除报警信息
        }
        if(g_humi > 80.0)
        {
            OledShowString(0, 6, "Humi Abnormal!!", 2);

        }
        else
        {
            OledShowString(0, 6, "                ", 2);
        }
        // if(g_gas_flag == 1)
        // {
        //     OledShowString(0, 6, "Gas Abnormal!!", 2);
        // }

        usleep(1 * 1000 * 1000);
    }
}

void Oled_entry(void)
{
    osThreadAttr_t attr;

    attr.name = "Oled_task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4 * 1024;
    attr.priority = osPriorityNormal;

    if (osThreadNew(Oled_task, NULL, &attr) == NULL) {
        printf("[Oled_task] Falied to create Oled_task!\n");
    }
}

SYS_RUN(Oled_entry);